import { defineStore } from 'pinia';

export type LoginInfo = {
  appID: string;
  uid: string;
  shortNo: string;
  token?: string;
  name: string;
  sex: string;
};

interface userStore {
  loginInfo: LoginInfo | null;
}

interface userGetters {
  loginInfo: () => LoginInfo;
}

interface userActions {
  setUser: (payload: LoginInfo) => void;
}

export const useUserStore = defineStore('user', {
  state: (): userStore => {
    return {
      loginInfo: {
        appID: '',
        uid: '',
        shortNo: '',
        token: '',
        name: '',
        sex: '',
      },
    };
  },

  actions: {
    setUser(payload: LoginInfo) {
      this.loginInfo = payload;
    },
  },
  persist: {
    key: 'userInfo',
    // 指定存储方式，默认为 localStorage
    storage: localStorage,
    // 指定要持久化的状态字段
    // paths: ['loginInfo'],
  },
});

export const useToken = defineStore('token', {
  state: () => {
    return {
      token: '',
    };
  },
  actions: {
    setToken(token: string) {
      this.token = token;
    },
  },
  persist: {
    key: 'token',
    // 指定存储方式，默认为 localStorage
    storage: localStorage,
    // 指定要持久化的状态字段
    // paths: ['loginInfo'],
  },
});
